package com.vf.admin.common.db.SqlUitls;

import java.beans.IntrospectionException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.HashMap;

import com.vf.admin.common.db.annotation.Column;
import com.vf.admin.common.db.annotation.NoColumn;
import com.vf.admin.common.db.annotation.Table;
import com.vf.admin.common.db.utils.PojoBySql;
import org.apache.commons.beanutils.PropertyUtilsBean;

public class PojoConverSql {
	public static PojoConverSql getInstance(){
		return new PojoConverSql();
	}
	/**
	 * 把pojo实体对象属性转换成插入的sql字段
	 *
	 * @throw Exception
	 */
	public void pojoBySql(Object obj, PojoBySql pojobysql) throws IllegalAccessException ,InvocationTargetException, NoSuchMethodException {
		Class clas = obj.getClass();
		String clasName = "";
		clasName = clas.getName();
		String tableName = "";
		HashMap<String, String> objTableMap = new HashMap<String, String>();
		HashMap<String, Object> columnDateMap = new HashMap<String, Object>();
		HashMap<String, Object> columnNameMap = new HashMap<String, Object>();
		HashMap<String, Object> columnDataNameMap = new HashMap<String, Object>();

		//类名称
		if (clasName.indexOf(".") >= 0) {
			clasName = clasName.substring(clasName.lastIndexOf(".") + 1,clasName.length());
			tableName = clasName;    //表名称做注解则默:类名为表名称
		}
		//类名称对应的表名称
		if(clas.isAnnotationPresent(Table.class)){
			Table table = (Table) clas.getAnnotation(Table.class);    //该class存在Table类型的注解，获取指定的表名
			tableName = table.name();

		}
		Field[] fieldArray = clas.getDeclaredFields();
		for (Field  field : fieldArray) {
			if(field.isAnnotationPresent(NoColumn.class)){
				continue;
			}
			String coluName = field.getName(); //成员变量名 如果字段没有注解默认为成员变量名 useCaches
			if(coluName.trim().equals("serialVersionUID")){
				continue ;
			}
			Class type = field.getType(); //成员变量类型
			Object data = null;    //成员变量值

			data = new PropertyUtilsBean().getProperty(obj, coluName);

			int coluLength = 32;    //默认为32位长度
			boolean index = false;    //默认不是主键
			String name_dataname = coluName;
			if(data!=null) {
				columnNameMap.put(coluName, data); //字段名称-值
				if (field.isAnnotationPresent(Column.class)) {
					Column column = field.getAnnotation(Column.class);//获取成员变量的注解实例
					name_dataname = column.name() + " " + coluName;
					coluName = column.name();    //变量对应的注解中的表字段名称
					coluLength = column.length();
					index = column.index();
					if (index == true) {
						pojobysql.setIndexName(coluName);
					}
				}
				columnDataNameMap.put(name_dataname, data);
				columnDateMap.put(coluName, data);    //字段名称-值
			}
//			}else{
//				columnNameMap.put(coluName, data); //字段名称-值
//				if(field.isAnnotationPresent(Column.class)){
//					Column column = field.getAnnotation(Column.class);//获取成员变量的注解实例
//					name_dataname = column.name() +" "+coluName;
//					coluName = column.name();    //变量对应的注解中的表字段名称
//					coluLength = column.length();
//					index = column.index();
//					if(index==true){
//						pojobysql.setIndexName(coluName);
//					}
//				}
//				columnDataNameMap.put(name_dataname, data);
//			}
		}
		pojobysql.setTableName(tableName);
		pojobysql.setColumnDateMap(columnDateMap);
		pojobysql.setColumnNameMap(columnNameMap);
		pojobysql.setColumnDataNameMap(columnDataNameMap);
	}

	/**
	 * 把pojo实体对象属性转换成插入的sql字段
	 *
	 * @throw Exception
	 */
	public void pojoBySql(Class clas, PojoBySql pojobysql) throws Exception,
			InvocationTargetException, NoSuchMethodException, SQLException, IntrospectionException {
		String clasName = "";
		clasName = clas.getName();
		String tableName = "";
		HashMap<String, String> objTableMap = new HashMap<String, String>();
		HashMap<String, Object> columnDateMap = new HashMap<String, Object>();
		HashMap<String, Object> columnNameMap = new HashMap<String, Object>();
		HashMap<String, Object> columnDataNameMap = new HashMap<String, Object>();

		//类名称
		if (clasName.indexOf(".") >= 0) {
			clasName = clasName.substring(clasName.lastIndexOf(".") + 1,clasName.length());
			tableName = clasName;    //表名称做注解则默:类名为表名称
		}
		//类名称对应的表名称
		if(clas.isAnnotationPresent(Table.class)){
			Table table = (Table) clas.getAnnotation(Table.class);    //该class存在Table类型的注解，获取指定的表名
			tableName = table.name();

		}
		Field[] fieldArray = clas.getDeclaredFields();
		for (Field  field : fieldArray) {
			if(field.isAnnotationPresent(NoColumn.class)){
				continue;
			}
			String coluName = field.getName(); //成员变量名 如果字段没有注解默认为成员变量名 useCaches
			if(coluName.trim().equals("serialVersionUID")){
				continue ;
			}
			Class type = field.getType(); //成员变量类型
			Object data = new PropertyUtilsBean().getProperty(clas.newInstance(), coluName);    //成员变量值
			int coluLength = 32;    //默认为32位长度
			boolean index = false;    //默认不是主键
			String name_dataname = coluName;
//			if(data!=null){
			columnNameMap.put(coluName, data); //字段名称-值
			if(field.isAnnotationPresent(Column.class)){
				Column column = field.getAnnotation(Column.class);//获取成员变量的注解实例
				name_dataname = column.name() +" "+coluName;
				coluName = column.name();    //变量对应的注解中的表字段名称
				coluLength = column.length();
				index = column.index();
				if(index==true){
					pojobysql.setIndexName(coluName);
				}
			}
			columnDataNameMap.put(name_dataname, data);
			columnDateMap.put(coluName,data);    //字段名称-值
//			}else{
//				columnNameMap.put(coluName, data); //字段名称-值
//				if(field.isAnnotationPresent(Column.class)){
//					Column column = field.getAnnotation(Column.class);//获取成员变量的注解实例
//					name_dataname = column.name() +" "+coluName;
//					coluName = column.name();    //变量对应的注解中的表字段名称
//					coluLength = column.length();
//					index = column.index();
//					if(index==true){
//						pojobysql.setIndexName(coluName);
//					}
//				}
//				columnDataNameMap.put(name_dataname, data);
//			}
		}
		pojobysql.setTableName(tableName);
		pojobysql.setColumnDateMap(columnDateMap);
		pojobysql.setColumnNameMap(columnNameMap);
		pojobysql.setColumnDataNameMap(columnDataNameMap);
	}
}
